﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace Acms.WebControls
{
    public class SimpleRepeater : CompositeDataBoundControl
    {
        protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
        {
            IEnumerator data = null;
            if (!dataBinding)
            {
                data = new object[TotalRecords].GetEnumerator();
            }
            else if (dataSource != null)
            {
                data = dataSource.GetEnumerator();
            }
            else
            {
                data = new object[0].GetEnumerator();
            }

            _Items = new List<SimpleRepeaterItem>();
            int i = 0;
            while (data.MoveNext())
            {
                CreateItem(i);
                i++;
            }

            if (dataBinding)
            {
                TotalRecords = _Items.Count;
                i = 0;
                data.Reset();
                while (data.MoveNext())
                {
                    _Items[i].SetDataItem(data.Current);
                    _Items[i].DataBind();
                    OnItemDataBound(new ItemDataBoundEventArgs(_Items[i], data.Current));
                    i++;
                }
            }
            return _Items.Count;
        }

        private void CreateItem(int itemIndex)
        {
            SimpleRepeaterItem item = new SimpleRepeaterItem(itemIndex);
            this.Controls.Add(item);
            _Items.Add(item);
            if (ItemTemplate != null)
            {
                ItemTemplate.InstantiateIn(item);
            }
            OnItemCreated(new ItemCreatedEventArgs(item));
        }

        protected void OnItemCreated(ItemCreatedEventArgs args)
        {
            if (ItemCreated != null)
            {
                ItemCreated(this, args);
            }
        }

        protected void OnItemDataBound(ItemDataBoundEventArgs args)
        {
            if (ItemDataBound != null)
            {
                ItemDataBound(this, args);
            }
        }

        public event EventHandler<ItemCreatedEventArgs> ItemCreated;
        public event EventHandler<ItemDataBoundEventArgs> ItemDataBound;

        private System.Web.UI.ITemplate itemTemplate;
        [
        Browsable(false),
        DefaultValue(null),
        Description("The item template."),
        TemplateContainer(typeof(SimpleRepeaterItem)),
        PersistenceMode(PersistenceMode.InnerDefaultProperty)
        ]
        public System.Web.UI.ITemplate ItemTemplate
        {
            get { return itemTemplate; }
            set { itemTemplate = value; }
        }

        public int TotalRecords { get; private set; }

        protected override object SaveViewState()
        {
            ViewState["TotalRecords"] = TotalRecords;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            int? tmpTotalRecords = ViewState["TotalRecords"] as int?;
            if (tmpTotalRecords.HasValue)
                TotalRecords = tmpTotalRecords.Value;
        }

        private List<SimpleRepeaterItem> _Items;
        public SimpleRepeaterItem[] Items
        {
            get 
            {
                if (_Items != null)
                {
                    return _Items.ToArray();
                }
                else
                {
                    return null;
                }
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            RenderContents(writer);
        }

    }

    public class SimpleRepeaterItem : WebControl, INamingContainer, IDataItemContainer
    {
        public SimpleRepeaterItem(int index)
        {
            ItemIndex = index;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            RenderContents(writer);
        }

        public int ItemIndex { get; private set; }

        #region IDataItemContainer Members

        internal void SetDataItem(object dataItem)
        {
            _DataItem = dataItem;
        }

        private object _DataItem;
        public object DataItem
        {
            get { return _DataItem; }
        }

        public int DataItemIndex
        {
            get { return ItemIndex; }
        }

        public int DisplayIndex
        {
            get { return ItemIndex; }
        }

        #endregion
    }

    public class ItemCreatedEventArgs : EventArgs
    {
        public ItemCreatedEventArgs(SimpleRepeaterItem item)
        {
            this.Item = item;
        }

        public SimpleRepeaterItem Item{ get; private set; }
    }

    public class ItemDataBoundEventArgs : ItemCreatedEventArgs
    {
        public ItemDataBoundEventArgs(SimpleRepeaterItem item, object dataItem)
            : base(item)
        {
            this.DataItem = dataItem;
        }

        public object DataItem { get; private set; }
    }
}
